package com.pearl.table.demo.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.table.demo.entity.OrderDetailEntity;
import com.pearl.table.demo.entity.OrderEntity;
import com.pearl.table.demo.mapper.OrderMapper;
import com.pearl.table.demo.mapper.OrderReturnMapper;
import com.pearl.table.demo.service.OrderDetailService;
import com.pearl.table.demo.service.OrderService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author 自动代码生成
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    private final OrderDetailService orderDetailService;

    private final OrderMapper orderMapper;

    @GetMapping("/page")
    public Object page() {
        return orderService.page(new Page<>(1, 10));
    }

    @GetMapping("/save")
    public Object save() {
        // 订单
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setProductId(1L);
        orderEntity.setOrderTime(LocalDateTime.now());
        orderService.save(orderEntity);
        // 订单详情
        OrderDetailEntity orderDetail=new OrderDetailEntity();
        orderDetail.setOrderId(orderEntity.getOrderId());
        orderDetail.setPhone(111111111111111L);
        orderDetail.setAddress("湖南永州");
        orderDetailService.save(orderDetail);
        return "操作成功";
    }

    @GetMapping("/list")
    public Object list() {
        // 订单、订单详情关联查询
        return orderMapper.selectOrderVO();
    }

    @GetMapping("/getById")
    public Object getById(@RequestParam Long id) {
        // 订单
        OrderEntity order = orderService.getById(id);
        // 订单号查询订单详情
        OrderDetailEntity orderDetail = orderDetailService.getOne(new LambdaQueryWrapper<OrderDetailEntity>().eq(OrderDetailEntity::getOrderId, order.getOrderId()));
        return "操作成功";
    }

    @GetMapping("/getByRange")
    public Object getByRange() {
        // ID 集合查询
        LambdaQueryWrapper<OrderEntity> queryWrapper = Wrappers.lambdaQuery();
        orderService.listByIds(ListUtil.toList(11l));
        // 时间范围查询
        queryWrapper.clear();
        queryWrapper.between(OrderEntity::getOrderTime, "2024-02-19 00:00:00", "2024-02-20 00:00:00");
        orderService.list(queryWrapper);
        return "查询成功";
    }

    private final OrderReturnMapper orderReturnMapper;

    @GetMapping("/getReturn")
    public Object getReturn() {
        orderReturnMapper.selectList(null);
        return "查询成功";
    }
}